
package com.microweb.toastmaster.service.impl;

import java.util.List;
import com.microweb.toastmaster.model.Agenda;
import com.microweb.toastmaster.dao.AgendaDao;
import com.microweb.toastmaster.model.Actor;
import com.microweb.toastmaster.model.MatchedAgenda;
import com.microweb.toastmaster.model.Query;
import com.microweb.toastmaster.model.User;
import com.microweb.toastmaster.model.viewwrapper.UserWrapper;
import com.microweb.toastmaster.service.AgendaManager;
import java.util.ArrayList;

public class AgendaManagerImpl extends BaseManager implements AgendaManager {
    //private Logger log = Logger.getLogger(AgendaManagerImpl.class.getName());
    private AgendaDao dao;

    /**
     * Set the Dao for communication with the data layer.
     * @param dao
     */
    public void setAgendaDao(AgendaDao dao) {
        this.dao = dao;
    }

    /**
     * @see com.microweb.toastmaster.service.AgendaManager#getAgendas(com.microweb.toastmaster.model.Agenda)
     */
    public List getAgendas(final Agenda agenda) {
        return dao.getAgendas(agenda);
    }

    /**
     * @see com.microweb.toastmaster.service.AgendaManager#getAgenda(String id)
     */
    public Agenda getAgenda(final String id) {
        return dao.getAgenda(new Long(id));
    }

    /**
     * @see com.microweb.toastmaster.service.AgendaManager#saveAgenda(Agenda agenda)
     */
    public void saveAgenda(Agenda agenda) {
        dao.saveAgenda(agenda);
    }

    /**
     * @see com.microweb.toastmaster.service.AgendaManager#removeAgenda(String id)
     */
    public void removeAgenda(final String id) {
        dao.removeAgenda(new Long(id));
    }
    
    /**
     * called by dwr
     */
    public UserWrapper getUserbyRole(String id, String rolename) {
        log.debug("in getUserbyRole()");
        Agenda agenda = dao.getAgenda(new Long(id));
        UserWrapper userwrapper = null;
        Actor actor = agenda.getActorByRolename(rolename);
        if (actor != null) {
            User user = actor.getUser();
            userwrapper = new UserWrapper(user, rolename);
            log.debug("find user: " + user.getUsername());
        }
        
        return userwrapper;
        
    }
    
    /**
     *	the way of get matched agenda is:
     *	1. get all agendas
     *  2. check each agenda to see if it match the query condition.
     *	    the match rule should be.
     */
    public List<MatchedAgenda> query(final Query query) {
        List<MatchedAgenda> results = new ArrayList<MatchedAgenda>();
	List<?> agendas = dao.getAgendas(null);
        log.debug("in query, agenda size: " + agendas.size());
	
	// empty, return empty list
	if(query.isEmpty())
	    return results;
	
	// role empty, but user be input.
	if(query.isRoleEmpty()) {
            log.debug("role is empty, query based on user: " + query.getUsername());
	    for (int i = 0; i<agendas.size(); i++) {
		Agenda agenda = (Agenda)agendas.get(i);
		List<?> actors = agenda.getActorsByUsername(query.getUsername());
		for(int j=0; j<actors.size(); j++) {
		    Actor actor= (Actor)actors.get(j);
		    MatchedAgenda matched = new MatchedAgenda();
		    matched.setId(agenda.getId());
		    matched.setDate(agenda.getDate());
		    matched.setRolename(actor.getTMRole().getName());
		    matched.setUsername(actor.getUser().getFullName());
		    results.add(matched);
		    log.debug("found a match: " + matched);
		}
	    }
            return results;
	}
	
	// user empty, but role be input.
	if(query.isUserEmpty()) {
            log.debug("user is empty, query based on role: " + query.getTmrolename());
	    for (int i = 0; i<agendas.size(); i++) {
		Agenda agenda = (Agenda)agendas.get(i);
		List<?> actors = agenda.getActorsByRolename(query.getTmrolename());
		for(int j=0; j<actors.size(); j++) {
		    Actor actor= (Actor)actors.get(j);
		    MatchedAgenda matched = new MatchedAgenda();
		    matched.setId(agenda.getId());
		    matched.setDate(agenda.getDate());
		    matched.setRolename(actor.getTMRole().getName());
		    matched.setUsername(actor.getUser().getFullName());
		    results.add(matched);
		    log.debug("found a match: " + matched);
		}
	    }
            return results;
	}
	
	// both user and role are input.
	for (int i = 0; i<agendas.size(); i++) {
            log.debug("query by user: " + query.getUsername() + ", role: " + query.getTmrolename());
		Agenda agenda = (Agenda)agendas.get(i);
		List<?> actors = agenda.getActorsByRoleAndUser(query.getTmrolename(),
			query.getUsername());
		for(int j=0; j<actors.size(); j++) {
		    Actor actor= (Actor)actors.get(j);
		    MatchedAgenda matched = new MatchedAgenda();
		    matched.setId(agenda.getId());
		    matched.setDate(agenda.getDate());
		    matched.setRolename(actor.getTMRole().getName());
		    matched.setUsername(actor.getUser().getUsername());
		    results.add(matched);
		    log.debug("found a match: " + matched);
		}
	}
	return results;
    }
}
